package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import model.Reject;
import util.DBUtil;
public class RejectDAO {
	public boolean save(Reject reject){
		DBUtil util = new DBUtil("insert into reject values(?,?,?,?,?,?,?)");
		try {
			util.pst.setString(1, reject.getRejectId());
			util.pst.setString(2, reject.getRejectCode());
			util.pst.setString(3, reject.getQuantity()+"");
			util.pst.setString(4, reject.getStatus());
			util.pst.setString(5, reject.getCreator());
			util.pst.setString(6, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(reject.getTs()));
			util.pst.setString(7, reject.getProductId());
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}

	public List<Reject> getByStatus(String status){
		List<Reject> rejectList = new ArrayList<Reject>();
		DBUtil util = new DBUtil("select * from reject where STATUS = ?");
		try {
			util.pst.setString(1, status);
			ResultSet rs = util.pst.executeQuery();
			while(rs.next()){
				Reject reject = new Reject(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getDate(6),rs.getString(7));
				rejectList.add(reject);
			}
			util.close();
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
		}
		return rejectList;
	}
	public boolean updateStatus(String status,String stockId){
		DBUtil util = new DBUtil("update reject set STATUS = ? where REJECT_ID = ?");
		try {
			util.pst.setString(1, status);
			util.pst.setString(2, stockId);
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}
	public boolean updateCodeAndCreator(String code,String creator,String id){
		DBUtil util = new DBUtil("update reject set `REJECT _CODE` = ? ,CREATOR = ? where REJECT_ID = ?");
		try {
			util.pst.setString(1, code);
			util.pst.setString(2, creator);
			util.pst.setString(3, id);
			util.pst.execute();
			util.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			util.close();
			return false;
		}
	}
}
